package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC939 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int minAreaRect(int[][] points) {
            Set<Integer> dict = new HashSet<>();
            for (int[] point : points) {
                int pointValue = point[0] * 40001 + point[1];
                dict.add(pointValue);
            }

            int minArea = Integer.MAX_VALUE;
            for (int i = 0; i < points.length - 1; i++) {
                for (int j = i + 1; j < points.length; j++) {
                    int x1 = points[i][0], x2 = points[j][0], y1 = points[i][1], y2 = points[j][1];
                    if (x1 != x2 && y1 != y2) {
                        int pv1 = 40001 * x2 + y1, pv2 = 40001 * x1 + y2;
                        if (dict.contains(pv1) && dict.contains(pv2)) {
                            int curArea = Math.abs((x1 - x2) * (y1 - y2));
                            minArea = Math.min(minArea, curArea);
                        }
                    }
                }
            }

            return minArea == Integer.MAX_VALUE ? 0 : minArea;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] arr = InputUtil.toDoubleIntegerArray("[[1,1],[1,3],[3,1],[3,3],[2,2]]");
        System.out.println(solution.minAreaRect(arr));
    }

    public static void main(String[] args) throws Exception {
        LC939 an = new LC939();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
